package com.bawei.mall.user.service.impl;

import com.bawei.common.security.utils.SecurityUtils;
import com.bawei.mall.user.domain.CusUser;
import com.bawei.mall.user.domain.CusUserCoupon;
import com.bawei.mall.user.domain.MallUserCollectInfo;
import com.bawei.mall.user.mapper.CusUserMapper;
import com.bawei.mall.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName UserServiceImpl
 * @Description 描述
 * @Author ZiZheng.Li
 * @Date 2024/6/28 9:51
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private CusUserMapper userMapper;
    @Override
    public CusUser selectUserByUserName(String username) {
        return userMapper.selectUserByUserName(username);
    }

    @Override
    public List<MallUserCollectInfo> selectCollectProductBrandInfoList(MallUserCollectInfo collect) {
        //获取当前登录人信息
        collect.setMemberId(SecurityUtils.getLoginCusUser().getCusUser().getUserId());
        return userMapper.selectCollectInfos(collect);
    }

    @Override
    public void clearCollectInfos(List<Integer> productIds) {
        //获取登录人ID
        MallUserCollectInfo mallUserCollectInfo = new MallUserCollectInfo();
        mallUserCollectInfo.setMemberId(SecurityUtils.getLoginCusUser().getCusUser().getUserId());
        //获取所收藏的商品信息
        List<MallUserCollectInfo> mallUserCollectInfos = selectCollectProductBrandInfoList(mallUserCollectInfo);
        for (MallUserCollectInfo userCollect : mallUserCollectInfos) {
            Integer productId = userCollect.getProductId();
            //TODO 有可能报错
            productIds.add(productId);
        }
        if (productIds.size() <= 0){
            throw new RuntimeException("没有要清空的商品信息");
        }
        //清空收藏商品信息 并且 修改对应商品收藏人气信息
        userMapper.clearCollectInfos(productIds);
    }

    @Override
    public List<CusUserCoupon> selectCouponInfos(String useStatus) {
        return userMapper.selectCouponInfos(useStatus);
    }

    @Override
    public List<MallUserCollectInfo> getAllCollectInfos() {
        log.info("====用户ID："+SecurityUtils.getUserId());
        //获取当前登录人信息
        if (SecurityUtils.getUserId() == null || SecurityUtils.getUserId() == 0){
            throw new RuntimeException("您还未登录,请尽快登录");
        }
        Long userId = SecurityUtils.getUserId();
        return userMapper.getAllCollectInfos(userId);
    }
}
